package cn.dao.Impl;

import cn.base.Impl.IBaseDaoImpl;
import cn.dao.TopicDao;
import cn.domain.Forum;
import cn.domain.Reply;
import cn.domain.Topic;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by Administrator on 2017/4/5 0005.
 */
@Repository
public class TopicDaoImpl extends IBaseDaoImpl<Topic> implements TopicDao {
    @Override
    public List<Topic> Show(Long id) {
        //进行排序
        String hql="from Topic where ForumId=? ORDER BY (case type when  2  then 2 else 1 end) desc , postTime Desc";
        Query query = session().createQuery(hql).setParameter(0,id);
        List<Topic> list=query.list();
        return list;
    }

    @Override
    public List<Reply> reply(Long id) {
        String hql="from Reply where TopicId=?";
        Query query = session().createQuery(hql).setParameter(0,id);
        List<Reply> list=query.list();
        return list;
    }

    @Override
    public void save(Topic entity) {
        Forum forum=entity.getForum();
        forum.setLastTopic(entity);
        entity.setForum(forum);
        super.save(entity);
    }
}
